import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'index',
    redirect: '/EnglishLearning',
    component: () => import(/* webpackChunkName: "about" */ '../views/main.vue'),
    children: [
      {
        path: '/userInfo',
        name: '个人中心',
        component: () => import(/* webpackChunkName: "about" */ '../views/userInfo'),
        isShow: true,
        meta: {
          name: '个人中心',
          iconType: 'book',
          type: 3
        }
      },
      {
        path: '/EnglishLearning',
        name: '英语学习',
        component: () => import(/* webpackChunkName: "about" */ '../views/EnglishLearning'),
        isShow: true,
        meta: {
          name: '英语学习',
          iconType: 'book',
          type: 3
        }
      },
      {
        path: '/EuropeanCup',
        name: '欧洲杯',
        component: () => import(/* webpackChunkName: "about" */ '../views/EuropeanCup'),
        isShow: true,
        meta: {
          name: '欧洲杯',
          iconType: 'deployment-unit',
          type: 3
        }
      },
      {
        path: '/StaffManagement',
        name: '员工管理',
        component: () => import(/* webpackChunkName: "about" */ '../views/StaffManagement'),
        isShow: true,
        meta: {
          name: '员工管理',
          iconType: 'team',
          type: 3
        }
      }
    ],
    meta: {
      // 1.随意访问的，不用登录   2.需要登录才能访问的   3.管理后台，侧面显示的（默认都是需要登录的）
      type: 3
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "about" */ '../views/LoginAndRegister/Login.vue'),
    isShow: false,
    meta: {
      name: 'Login',
      iconType: 'video-camera',
      type: 1
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import(/* webpackChunkName: "about" */ '../views/LoginAndRegister/Register.vue'),
    isShow: false,
    meta: {
      name: 'Register',
      iconType: 'video-camera',
      type: 1
    }
  },
  {
    path: '/notFound',
    name: 'notFound',
    component: () => import(/* webpackChunkName: "about" */ '../views/404.vue'),
    isShow: false,
    meta: {
      name: 'notFound',
      iconType: 'video-camera',
      type: 1
    }
  }
]

const router = new VueRouter({
  linkActiveClass: 'active',
  mode: 'history',
  routes
})

const routesObj = {
  type01List: [],
  type03List: [],
  type02List: []
}

function getType (item) {
  if (item.meta.type === 1) {
    routesObj.type01List.push(item.path)
  } else if (item.meta.type === 2) {
    routesObj.type02List.push(item.path)
  } else if (item.meta.type === 3) {
    routesObj.type03List.push(item.path)
  }
}
routes.forEach(item => {
  if (item.path) {
    getType(item)
  }
  if (item.children?.length > 0) {
    item.children.forEach(item02 => {
      if (item02.path) {
        getType(item02)
      }
    })
  }
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) { return originalPush.call(this, location, onResolve, onReject) }
  return originalPush.call(this, location).catch((err) => err)
}

router.beforeEach((to, from, next) => {
  // console.log(to)
  // console.log(routesObj)
  store.commit('user/SET_ROUTER', [to.path])
  store.commit('user/GET_USER_INFO')
  store.commit('user/loadTokensFromLocalStorage')

  // 路由三种情况：1.随意访问的； 2.需要登陆才能访问的； 3.管理后台，侧面显示的
  if (routesObj.type01List.includes(to.path) || routesObj.type02List.includes(to.path) || routesObj.type03List.includes(to.path)) {
    if (routesObj.type02List.includes(to.path) || routesObj.type03List.includes(to.path)) {
      const loginSession = localStorage.getItem('accessToken')
      if (loginSession) return next()
      next({ path: '/login' })
    }
    next()
  } else {
    next({ path: '/notFound' })
  }
})

export default router
